package com.ruoyi.mp.controller;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.EncryptAesUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.web.MpClientTransfer;
import com.ruoyi.mp.annotion.Login;
import com.ruoyi.mp.annotion.LoginUser;
import com.ruoyi.mp.service.IMpClientTransferService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 用户转账记录Controller
 * 
 * @author ruoyi
 * @date 2022-07-27
 */
@Api(value = "用户转账记录",tags ="用户转账记录")
@RestController
@RequestMapping("/smp/transfer")
public class MpClientTransferController extends BaseController
{
    private static Logger log = LoggerFactory.getLogger(MpClientTransferController.class);
    @Autowired
    private IMpClientTransferService mpClientTransferService;

    /**
     * 查询用户转账记录列表
     */
    @Login
    @ApiOperation(value = "查询用户转账记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClientTransfer",value="用户转账记录",paramType="form")
    })
    @GetMapping("/list")
    public TableDataInfo list(MpClientTransfer mpClientTransfer,@LoginUser MpClient client)
    {
        try {
            startPage();
            mpClientTransfer.setClientId(client.getClientId());
            List<MpClientTransfer> list = mpClientTransferService.selectMpClientTransferList(mpClientTransfer);
            return getDataTable(list);
        }catch (Exception e){
            log.error("用户转账记录发生异常",e);
        }
        return null;
    }



    /**
     * 获取用户转账记录详细信息
     */
    @ApiOperation(value = "获取用户转账记录详细信息")
    @ApiImplicitParam(name="id",value="用户转账记录")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        try {
            return AjaxResult.success(mpClientTransferService.selectMpClientTransferById(id));
        }catch (Exception e){
            log.error("用户转账记录发生异常",e);
        }
        return AjaxResult.error("用户转账记录异常");
    }

    /**
     * 新增用户转账记录
     */
    @Login
    @ApiOperation(value = "新增用户转账记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mpClientTransfer",value="用户转账记录",dataType="MpClientTransfer")
    })
    @Log(title = "用户转账记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MpClientTransfer mpClientTransfer, @LoginUser MpClient client)
    {
        if(StringUtils.isBlank(client.getPayPassword())){
            throw new ServiceException("请先设置交易密码");
        }
        String entryPwd = EncryptAesUtil.encrypt(mpClientTransfer.getPayPassword());
        if(entryPwd==null||!client.getPayPassword().equals(entryPwd)){
            throw new ServiceException("交易密码错误");
        }
        try {
            if(client.getIsVirtual()!=null&&client.getIsVirtual()==1){
                return AjaxResult.success("虚拟号无法转账");
            }
            mpClientTransfer.setClientId(client.getClientId());
            mpClientTransferService.insertMpClientTransfer(mpClientTransfer,client);
            return AjaxResult.success("转账成功");
        }catch (Exception e){
            log.error("新增用户转账记录发生异常",e);
            throw new ServiceException(e.getMessage());
        }
    }
    /**
     * 查询用户转账手续费比例
     */
    @Login
    @ApiOperation(value = "查询用户转账手续费比例")
    @GetMapping("/getConfig/rate")
    public AjaxResult getRate(@LoginUser MpClient client)
    {
        try {
            BigDecimal rate = mpClientTransferService.getTransferRate();
            return AjaxResult.success(rate);
        }catch (Exception e){
            log.error("用户转账记录发生异常",e);
        }
        return AjaxResult.error("用户转账记录异常");
    }
    /**
     * 查询用户转账手续费比例(银藻)
     */
    @Login
    @ApiOperation(value = "查询用户银藻转账手续费比例")
    @GetMapping("/getConfig/SliverRate")
    public AjaxResult getSliverRate(@LoginUser MpClient client)
    {
        try {
            BigDecimal rate = mpClientTransferService.getTransferSliverRate();
            return AjaxResult.success(rate);
        }catch (Exception e){
            log.error("用户转账记录发生异常",e);
        }
        return AjaxResult.error("用户转账记录异常");
    }
}
